package com.jiyi.activity.animation;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.Keyframe;
import android.animation.LayoutTransition;
import android.animation.ObjectAnimator;
import android.animation.PropertyValuesHolder;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.jiyi.widget.AutoLinefeedLayout;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


public class AnimationActivity extends ActivityBase {

    @BindView(R.id.ivDisplay)
    ImageView ivDisplay;

    @BindView(R.id.allMain)
    AutoLinefeedLayout allMain;

    @BindView(R.id.fabBtn)
    FloatingActionButton fabBtn;

    @BindView(R.id.rlRoot)
    RelativeLayout rlRoot;



    int curBtn = 1;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_animation);
        ButterKnife.bind(this);
    }


    @OnClick({R.id.btnAlpha, R.id.btnRotate, R.id.btnScale, R.id.btnTranslate,R.id.btnTween,R.id.btnDrawable
    ,R.id.btnDrawableRotate,R.id.btnPro,R.id.btnViewProAni,R.id.btnLayoutAni,R.id.btnRevearEffect,R.id.fabBtn})
    public void OnClick(View view) {
        Animation animation;
        AnimationDrawable animationDrawable;
        switch (view.getId()) {
            case R.id.btnAlpha:
                animation = AnimationUtils.loadAnimation(this, R.anim.alpha_test);
                animation.setDuration(2000);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(4);
                ivDisplay.startAnimation(animation);
                break;
            case R.id.btnRotate:
                animation = AnimationUtils.loadAnimation(this, R.anim.rotate_test);
                animation.setDuration(2000);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(2);
                ivDisplay.startAnimation(animation);
                break;
            case R.id.btnScale:
                animation = AnimationUtils.loadAnimation(this, R.anim.scale_test);
                animation.setDuration(2000);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(2);
                ivDisplay.startAnimation(animation);
                break;
            case R.id.btnTranslate:
                animation = AnimationUtils.loadAnimation(this, R.anim.translate_test);
                animation.setDuration(2000);
                animation.setRepeatMode(Animation.REVERSE);
                animation.setRepeatCount(3);
                ivDisplay.startAnimation(animation);
                break;
                case R.id.btnTween:
                animation = AnimationUtils.loadAnimation(this, R.anim.tween_test);
                ivDisplay.startAnimation(animation);
                break;
            case  R.id.btnDrawable:
                ivDisplay.setImageResource(R.drawable.anim_drawable_test);
                animationDrawable = (AnimationDrawable) ivDisplay.getDrawable();
                animationDrawable.start();
                break;
                case  R.id.btnDrawableRotate:
                ivDisplay.setImageResource(R.drawable.anim_drawable_rotate);
                break;
            case R.id.btnPro:


                //单个属性修改
//                ObjectAnimator ob = ObjectAnimator.ofFloat(ivDisplay,"rotationX",0,360).setDuration(5000);
//                ob.start();

                //多个属性修改
//                PropertyValuesHolder x = PropertyValuesHolder.ofFloat("x",0,100);
//                PropertyValuesHolder rotateY = PropertyValuesHolder.ofFloat("rotationY",0,180);
//                PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX",1,2);
//                ObjectAnimator.ofPropertyValuesHolder(ivDisplay,x,rotateY,scaleX).setDuration(3000).start();

                //关键帧
                Keyframe k1 = Keyframe.ofFloat(0F,0);
                Keyframe k2 = Keyframe.ofFloat(0.2F,-30);
                Keyframe k3 = Keyframe.ofFloat(0.4F,30);
                Keyframe k4 = Keyframe.ofFloat(0.6F,-30);
                Keyframe k5 = Keyframe.ofFloat(0.8F,30);
                Keyframe k6 = Keyframe.ofFloat(1F,0);

                PropertyValuesHolder propertyValuesHolder = PropertyValuesHolder.ofKeyframe("rotation",k1,k2,k3,k4,k5,k6);

                Keyframe f1 = Keyframe.ofFloat(0F,1);
                Keyframe f2 = Keyframe.ofFloat(0.5F,2F);
                Keyframe f3 = Keyframe.ofFloat(1F,1);

                PropertyValuesHolder propertyValuesHolder1 = PropertyValuesHolder.ofKeyframe("ScaleX",f1,f2,f3);

                ObjectAnimator.ofPropertyValuesHolder(ivDisplay,propertyValuesHolder,propertyValuesHolder1).setDuration(2000).start();

                break;
            case R.id.btnViewProAni:
                ivDisplay.animate().xBy(100).alpha(0.5F).rotation(180).scaleY(1.5F).setDuration(5000);
                break;
            case R.id.btnLayoutAni:
                Button button = new Button(this);
                button.setText("Layout Visity "+curBtn);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                button.setLayoutParams(layoutParams);

                LayoutTransition layoutTransition = new LayoutTransition();
                PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha",0,1);
                PropertyValuesHolder sx = PropertyValuesHolder.ofFloat("ScaleX",0,1);
                PropertyValuesHolder sy = PropertyValuesHolder.ofFloat("ScaleX",0,1);
                layoutTransition.setAnimator(LayoutTransition.APPEARING,ObjectAnimator.ofPropertyValuesHolder(button,alpha,sx,sy).setDuration(3000));

                if (allMain.getLayoutTransition() == null)
                {
                    allMain.setLayoutTransition(layoutTransition);
                }
                allMain.addView(button);
                curBtn++;
                break;
            case R.id.btnRevearEffect:
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    //圆形缩放
                    Animator animator = ViewAnimationUtils.createCircularReveal(ivDisplay,ivDisplay.getWidth()/2,ivDisplay.getHeight()/2,ivDisplay.getWidth()/2,0);
                    animator.setDuration(5000);
                    animator.addListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            ivDisplay.setVisibility(View.GONE);
                        }
                    });
                    animator.start();
                }
                break;
            case R.id.fabBtn:
                // 获取FloatingActionButton的中心点的坐标
                int centerX = (fabBtn.getLeft() + fabBtn.getRight()) / 2;
                int centerY = (fabBtn.getTop() + fabBtn.getBottom()) / 2;
//                int winX = DeviceUtil.getInstance().getScreenWidth()/2;
//                int winY =  DeviceUtil.getInstance().getScreenHeight()/2;
                // Math.hypot(x,y):  返回sqrt(x2 +y2)
                // 获取扩散的半径
                float finalRadius = (float) Math.hypot((double) centerX, (double) centerY);
                // 定义揭露动画
                Animator mCircularReveal = null;
                if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                    mCircularReveal = ViewAnimationUtils.createCircularReveal(
                            rlRoot, centerX, centerY, 0, finalRadius);
                }
                // 设置动画持续时间，并开始动画
                mCircularReveal.setDuration(4000).start();
                break;
        }
    }
}
